/**
 * @Title: AttentionDynamicsActivity.java
 * @Copyright: XXX Co., Ltd. Copyright YYYY-YYYY,  All rights reserved
 * @Description: TODO<请描述此文件是做什么的>
 * @author: MeiYi
 * @data: 2015-12-7 上午10:20:14
 * @version: V1.0
 */

package artid.com.arttogo.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.android.volley.VolleyError;

import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.activity.CollectionDetailPagerActivity;
import artid.com.arttogo.adapter.PersonCommentAdapter;
import artid.com.arttogo.bean.MyComment;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.AppUtil;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.view.pulllist.PullToRefreshLayout;

/**
 * TODO<请描述这个类是干什么的>
 *
 * @author 熊彬
 * @data: 2015-12-7 上午10:20:14
 * @version: V1.0
 */
public class CommentFragment extends Activity {
    PersonCommentAdapter adapter;
    //	PullToRefreshLayout mPullLayout;
    ListView listView;
    String id;
    int pageindex = 1, pagesize = 20;
    PullToRefreshLayout mPullLayout;
    View mViewComment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_comment);
        listView = (ListView) findViewById(R.id.listview);
        mViewComment = (View) findViewById(R.id.comment_blank);
        findViewById(R.id.back_btn).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                finish();
            }
        });

        mPullLayout = (PullToRefreshLayout) findViewById(R.id.refresh_view);
        mPullLayout.setVisibility(View.INVISIBLE);
        mPullLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                // TODO Auto-generated method stub
                pageindex = 1;
                reqData();
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                // TODO Auto-generated method stub
                // pullToRefreshLayout.setBottomDismiss();
                pageindex++;
                reqData();
            }
        });
        reqData();
    }


    void reqData() {
        VolleyBasic.getInstance(CommentFragment.this).post(
                Cache.myComment,
                VolleyMap.getInstance().myComment(AppUtil.getUser().getToken(),
                        String.valueOf(pageindex), String.valueOf(pagesize)),
                MyComment.class, new VolleyCallback() {

                    @Override
                    public void onSuccess(VolleyData vd) {
                        // TODO Auto-generated method stub

                        mPullLayout.loadmoreFinish(mPullLayout.SUCCEED);
                        final List<MyComment> list = (List<MyComment>) vd
                                .getList();

                        if (pageindex != 1) {
                            adapter.addlist(list);
                            listView.setOnItemClickListener(new OnItemClickListener() {

                                @Override
                                public void onItemClick(AdapterView<?> arg0,
                                                        View arg1, int arg2, long arg3) {
                                    // TODO Auto-generated method stub
                                    Intent intent = new Intent(CommentFragment.this, CollectionDetailPagerActivity.class);
                                    intent.putExtra("id", adapter.listdata.get(arg2).getArt_id());
                                    intent.putExtra("titleName", adapter.listdata.get(arg2).getTitle());
                                    startActivity(intent);
                                }
                            });
                            return;
                        }
                        if (list.isEmpty() && pageindex == 1) {
                            mViewComment.setVisibility(View.VISIBLE);
                            return;
                        }
                        mViewComment.setVisibility(View.GONE);
                        mPullLayout.setVisibility(View.VISIBLE);
                        adapter = new PersonCommentAdapter(CommentFragment.this);
                        listView.setAdapter(adapter);
                        adapter.setdata(list);
                        listView.setOnItemClickListener(new OnItemClickListener() {

                            @Override
                            public void onItemClick(AdapterView<?> arg0,
                                                    View arg1, int arg2, long arg3) {
                                // TODO Auto-generated method stub
                                Intent intent = new Intent(CommentFragment.this, CollectionDetailPagerActivity.class);
                                intent.putExtra("id", adapter.listdata.get(arg2).getArt_id());
                                intent.putExtra("titleName", adapter.listdata.get(arg2).getTitle());
                                startActivity(intent);
                            }
                        });
                    }

                    @Override
                    public void onError(VolleyError volleyError) {
                        // TODO Auto-generated method stub
//						mPullLayout.loadmoreFinish(mPullLayout.FAIL);
                        mPullLayout.loadmoreFinish(mPullLayout.FAIL);
                    }
                });
    }
}
